package xyz.xpman.prisonerfitness.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import java.util.Collections;
import java.util.List;

import xyz.xpman.prisonerfitness.R;
import xyz.xpman.prisonerfitness.activity.base.BaseActivity;
import xyz.xpman.prisonerfitness.adapter.ChapterAdapter;
import xyz.xpman.prisonerfitness.common.App;
import xyz.xpman.prisonerfitness.config.Constant;
import xyz.xpman.prisonerfitness.entity.Chapter;
import xyz.xpman.prisonerfitness.entity.Mappings;

@RequiresApi(api = Build.VERSION_CODES.O)
public class IndexActivity extends BaseActivity {
    private List<Chapter> items = Collections.emptyList();
    private ChapterAdapter chapterAdapter;
    private ListView chaptersListView;


    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        initData();
        initView();
    }


    private void initData() {
        items = Mappings.getChapters();
    }

    private void initView() {
        chaptersListView = findViewById(R.id.chapters);

        chapterAdapter = new ChapterAdapter(this, R.layout.chapter_temp, items);


        chaptersListView.setOnItemClickListener((parent, view, position, id) -> {
            Chapter item = (Chapter) chapterAdapter.getItem(position);

            Intent intent = newIntent(MovesActivity.class);
            intent.putExtra(Constant.EXTRA_CHAPTER_ID, item.getId());
            startActivity(intent);
        });

        runOnUiThread(() -> chaptersListView.setAdapter(chapterAdapter));
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.index_menu, menu);

        // 训练日记
        menu.findItem(R.id.training_diary).setOnMenuItemClickListener(view-> {
            toActivity(TrainingDiaryActivity.class);
            return true;
        });


        // 设置
        menu.findItem(R.id.setting).setOnMenuItemClickListener(view-> {
            toActivity(SettingActivity.class);
            return true;
        });


        // 关于
        menu.findItem(R.id.about).setOnMenuItemClickListener(view-> {
            toActivity(AboutActivity.class);
            return true;
        });


        // 捐助
        menu.findItem(R.id.donate).setOnMenuItemClickListener(view-> {
            Intent intent = newIntent(DonateWindowActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            return true;
        });

        return true;
    }

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



    // 点击两次返回退出app
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        return super.onKeyDown(keyCode, event);
        return twoKeyBackToExist(keyCode, event);
    }

    @Override
    protected void processBeforeExit() {
        Log.i("Index", "processBeforeExit...");
        super.processBeforeExit();
    }


    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


    @Override
    protected void onDestroy() {
        Log.i("Index", "onDestroy...");
        App.destroy();

        super.onDestroy();

        finishAndRemoveTask();

        System.exit(0);
        // 清楚运行记录
        // android.os.Process.killProcess(android.os.Process.myPid());
    }
}